package com.csust.automaticteach.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csust.automaticteach.annotation.AuthCheck;
import com.csust.automaticteach.common.BaseResponse;
import com.csust.automaticteach.common.DeleteRequest;
import com.csust.automaticteach.common.ResultUtils;
import com.csust.automaticteach.constant.UserAuthConstant;
import com.csust.automaticteach.exception.BusinessException;
import com.csust.automaticteach.exception.ErrorCode;
import com.csust.automaticteach.exception.ThrowUtils;
import com.csust.automaticteach.model.dto.classes.ClassesAddRequest;
import com.csust.automaticteach.model.dto.classes.ClassesQueryRequest;
import com.csust.automaticteach.model.dto.classes.ClassesUpdateRequest;
import com.csust.automaticteach.model.entity.Classes;
import com.csust.automaticteach.model.entity.ExercisesSets;
import com.csust.automaticteach.model.entity.User;
import com.csust.automaticteach.model.vo.classes.ClassesVO;
import com.csust.automaticteach.model.vo.classes.classteacher.TeacherClassesVO;
import com.csust.automaticteach.model.vo.exercises.ExercisesVO;
import com.csust.automaticteach.service.*;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName ClassesController
 * @Description 班级相关接口
 * @Author 钰玟
 * @Date 2025/8/16 下午4:07
 * Version 1.0
 **/
@RestController
@RequestMapping("/classes")
@Slf4j
public class ClassesController {
    @Resource
    private ClassesService classesService;
    @Resource
    private ClassTeacherService classTeacherService;
    @Resource
    private ClassExerciseService classExerciseService;
    @Resource
    private UserService userService;
    @Resource
    private ExercisesSetsService exercisesSetsService;

    // region 基本的增删改查

    /**
     * 管理员查询所有的班级
     */
    @PostMapping("/list/page/vo")
    @AuthCheck(mustRole = UserAuthConstant.ADMIN_ROLE)
    public BaseResponse<Page<ClassesVO>> listClassesVOByPage(@RequestBody ClassesQueryRequest classesQueryRequest) {
        //1.参数校验
        ThrowUtils.throwIf(classesQueryRequest == null, ErrorCode.PARAMS_ERROR);
        //2.获取分页参数
        int current = classesQueryRequest.getCurrent();
        int pageSize = classesQueryRequest.getPageSize();
        //3.分页查询
        Page<Classes> page = classesService.page(new Page<>(current, pageSize), classesService.getClassesQueryWrapper(classesQueryRequest));
        //4.转换
        Page<ClassesVO> classesVOPage = new Page<>(current, pageSize);
        classesVOPage.setRecords(classesService.getClassesVOList(page.getRecords()));
        classesVOPage.setTotal(page.getTotal());
        //5.返回
        return ResultUtils.success(classesVOPage);
    }

    /**
     * 创建班级，只添加了班主任，无其他添加
     *
     * @param classesAddRequest 添加的班级基本信息
     * @param request           请求
     * @return 添加结果
     */
    @PostMapping("/add")
    @AuthCheck(mustRole = UserAuthConstant.ADMIN_ROLE)
    public BaseResponse<Long> addClasses(@RequestBody ClassesAddRequest classesAddRequest, HttpServletRequest request) {
        //1.判断参数
        ThrowUtils.throwIf(classesAddRequest == null, ErrorCode.PARAMS_ERROR);
        //2.属性拷贝
        Classes classes = new Classes();
        BeanUtil.copyProperties(classesAddRequest, classes);
        //3.插入数据库
        boolean result = classesService.save(classes);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        //4.返回
        return ResultUtils.success(classes.getId());
    }

    /**
     * 删除班级
     *
     * @param deleteRequest 删除参数
     * @param request       请求
     * @return 删除结果
     */
    @PostMapping("/delete")
    @AuthCheck(mustRole = UserAuthConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> deleteClass(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(deleteRequest == null || deleteRequest.getId() <= 0, ErrorCode.PARAMS_ERROR);
        boolean b = classesService.removeById(deleteRequest.getId());
        return ResultUtils.success(b);
    }

    /**
     * 修改班级的基本信息
     *
     * @param classesUpdateRequest 修改的班级信息
     * @param request              请求
     * @return 修改结果
     */
    @PostMapping("/update")
    @AuthCheck(mustRole = UserAuthConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateClass(@RequestBody ClassesUpdateRequest classesUpdateRequest, HttpServletRequest request) {
        //1.参数校验
        Long id = classesUpdateRequest.getId();
        ThrowUtils.throwIf(classesUpdateRequest == null || id <= 0, ErrorCode.PARAMS_ERROR);
        //2.拷贝参数
        Classes classes = new Classes();
        BeanUtil.copyProperties(classesUpdateRequest, classes);
        //3. 查看数据是否存在
        Classes classesServiceById = classesService.getById(id);
        if (classesServiceById == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "班级不存在");
        }
        //4.修改数据库
        boolean update = classesService.updateById(classes);
        ThrowUtils.throwIf(!update, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    // endregion

    /**
     * 教师获取其教学的所有班级
     *
     * @param request HttpServletRequest
     * @return List<TeacherClassesVO>
     */
    @GetMapping("/get/teacher/class")
    public BaseResponse<List<TeacherClassesVO>> getClassesByTeacherId(HttpServletRequest request) {
        //1. 获取当前用户
        User currentUser = userService.getLoginUser(request);
        //2. 获取当前教师其下的所有班级
        List<TeacherClassesVO> teacherClassesVOList = classTeacherService.listClassesByTeacherId(currentUser.getId());
        //3.返回
        return ResultUtils.success(teacherClassesVOList);
    }

    /**
     * 获取班级的练习
     *
     * @param request HttpServletRequest
     * @return List<ExercisesSets>
     */
    @GetMapping("/get/class/exercise_sets")
    public BaseResponse<List<ExercisesSets>> listClassExerciseSets(HttpServletRequest request) {
        //1.获取当前用户
        User currentUser = userService.getLoginUser(request);
        //2.得到用户id  即教师id
        Long teacherId = currentUser.getId();
        //3.获取班级的练习
        List<ExercisesSets> exercisesSets = classExerciseService.listExerciseSetsByTeacherId(teacherId);
        return ResultUtils.success(exercisesSets);
    }

    @GetMapping("/get/class/exercise")
    public BaseResponse<List<ExercisesVO>> getClassExerciseSets(@RequestParam("setId") Long setId) {
        //1.校验参数
        ThrowUtils.throwIf(setId == null || setId <= 0, ErrorCode.PARAMS_ERROR);
        //2.调用 service 层的方法 获取套题的详情题目
        List<ExercisesVO> exercisesVOList = exercisesSetsService.getExerciseVOList(setId);
        //3.返回结果
        return ResultUtils.success(exercisesVOList);
    }

}
